import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class MyTest {

    private static String bg_img = "D:\\img\\bg.png";
    private static String code_img = "D:\\img\\code.png";

    private static String name = "北京白马城堡酒店";

    private static String address = "北京公安";
    private static String area = "（密云区）";


    public static void main(String[] args) {
        try {
            long startTime = System.currentTimeMillis();

            File destFile = new File("D:\\img\\ok.png");

            BufferedImage img = new BufferedImage(1206, 1772, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = (Graphics2D) img.getGraphics();

            BufferedImage bg = ImageIO.read(new File(bg_img));

//            BufferedImage content = ImageIO.read(new File(code_img));
//            BufferedImage content = ImageIO.read(new File("http://tfastdb.110passport.com/group1/M00/00/01/rBHj2V3Oi1iAdVHWAABQyXn0lMc160.png"));

            String path = hh("http://tfastdb.110passport.com/group1/M00/00/01/rBHj2V3Oi1iAdVHWAABQyXn0lMc160.png","D:\\img\\code.png");
            BufferedImage content = ImageIO.read(new File(path));

            g.drawImage(bg.getScaledInstance(1206, 1772, Image.SCALE_DEFAULT), 0, 0, null);



            g.drawImage(content.getScaledInstance(670, 670, Image.SCALE_DEFAULT), 268, 624, null);

            double width = (1206 - (name.length() * 106.0) ) /2 ;
            System.out.println(width);

            g.setColor(new Color(255, 255, 255));
            g.setFont(FontUtil.getFont(FontUtil.SIMSUN, 106.0f));
            g.drawString(name, new Double(width).intValue(), 584);



            g.setColor(new Color(255, 255, 255));
            g.setFont(FontUtil.getFont(FontUtil.SIMSUN, 58.0f));
            g.drawString(address, 60, 340);

            g.setColor(new Color(255, 255, 255));
            g.setFont(FontUtil.getFont(FontUtil.SIMSUN, 44.0f));
            g.drawString(area, 80, 400);


            g.dispose();
            ImageIO.write(img, "png", destFile);
            System.out.println("生成成功！");
            System.out.println("耗时: " + (System.currentTimeMillis() - startTime) / 1000.0 + "s");
            System.out.println("生成文件路径: " + destFile.getAbsolutePath());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String hh(String url,String outpath) {
        try {
            URL url1 = new URL(url);
            URLConnection uc = url1.openConnection();
            InputStream inputStream = uc.getInputStream();

            FileOutputStream out = new FileOutputStream(outpath);
            int j = 0;
            while ((j = inputStream.read()) != -1) {
                out.write(j);
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return outpath;
    }
}
